package com.example.demo.study.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

@SuppressWarnings(value =  {"unchecked","rawtypes"})
@Component
public class RedisCache {

    @Autowired
    public RedisTemplate redisTemplate;

    /**
     * 缓存基本的对象
     * @param key
     * @param value
     * @param <T>
     */
    public <T> void setCacheObject(final String key, final T value){
        redisTemplate.opsForValue().set(key,value) ;
    }

    public <T> void setCacheObject(final String key, final T value , final Integer timeout, final TimeUnit timeUnit){
        redisTemplate.opsForValue ().set(key, value, timeout, timeUnit);
    }

    public boolean expire(final String key, final long timeout){
        return expire(key, timeout, TimeUnit.SECONDS);
    }

    /**
     * 设置有效时间
     * @param key
     * @param timeout
     * @param unit
     * @return
     */
    public boolean expire(final String key, final long timeout, final TimeUnit unit){
        return expire(key, timeout, TimeUnit.SECONDS);
    }


    /**
     * 获得缓存的基本对象
     * @param key
     * @param <T>
     * @return
     */
    public <T> T getCacheObject(final String key){
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        return operation .get(key);
    }

    /**
     * 删除单个对象
     * @param key
     * @return
     */
    public boolean deleteObject(final String key){
        return redisTemplate .delete(key) ;
    }

    /**
     * 删除集合对象
     * @param collection 多个对象
     * @return
     */
    public long deleteObject(final Collection collection){
        return redisTemplate .delete(collection) ;
    }

    /**
     * 缓存 list 数据
     * @param key
     * @param dataList
     * @param <T>
     * @return
     */
    public <T> long setCacheList(final String key, final List<T> dataList){
        Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
        return count == null ? 0 : count;
    }

    /**
     * 获得 list 对象
     * @param key
     * @param <T>
     * @return
     */
    public <T> List<T> getListCache(final String key){
        return redisTemplate.opsForList().range(key,0,-1);
    }

    /**
     * 缓存 set
     * @param key
     * @param dataSet
     * @param <T>
     * @return
     */
    public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet){
        BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
        Iterator<T> it = dataSet.iterator();
        while (it.hasNext ()){
            setOperation.add(it.next());
        }
        return setOperation ;
    }

    public <T> Set<T> getCacheSet(final String key){
        return redisTemplate.opsForSet().members(key) ;
    }

    /**
     * 缓存 map
     * @param key
     * @param dataMap
     * @param <T>
     */
    public <T> void setCacheMap(final String key, final Map<String,T> dataMap){
        if (dataMap != null) {
            redisTemplate.opsForHash().putAll(key,dataMap);
        }
    }

    public <T> Map<String,T> getCacheMap(final String key){
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 往 hash 存数据
     * @param key
     * @param hkey
     * @param value
     * @param <T>
     */
    public <T> void setCacheMapValue(final String key, final String hkey,final T value){
        redisTemplate.opsForHash().put(key,hkey,value);
    }

    public <T> T getCacheMapValue(final String key, final String hkey){
        HashOperations<String,String,T> hashOperations = redisTemplate.opsForHash();
        return hashOperations.get(key,hkey);
    }

    /**
     * 删除 hash 中的数据
     * @param key
     * @param hkey
     * @param value
     * @param <T>
     */
    public <T> void delCacheMapValue(final String key, final String hkey,final T value){
        HashOperations<String,String,T> hashOperations = redisTemplate.opsForHash();
        hashOperations.delete(key,hkey);
    }

    /**
     * 获取多个 hash 中的数据
     * @param key
     * @param hkeys
     * @param <T>
     * @return
     */
    public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hkeys){
        return redisTemplate.opsForHash().multiGet(key,hkeys);
    }

    /**
     * 获取缓存的基本对象列表
     * @param pattern
     * @return
     */
    public Collection<String> keys(final String pattern){
        return redisTemplate.keys(pattern);
    }
}
